package CelestialWorkflow;

import java.sql.*;         //provides connection object and sql exception

import CelestialDataAccess.*;

public class LoginControl {

    public static final int NO_EMAIL = 1;         // no email supplied
    public static final int NO_PASSWORD = 2;      // no password supplied
    public static final int EMAIL_NOT_FOUND = 3;  // no records found
    public static final int WRONG_PASSWORD = 4;   // password mismatch
    public static final int INVALID_EMAIL = 5;    // email syntax is worng
    public static final int SYSTEM_ERROR = 6;     // system error, see message
    
    private int m_iError = 0;
    private String m_strErrorMessage = "";
    /*************************************************************************/
    /* get/set methods */
    public int getError(){ return m_iError; }
    public String getErrorMessage(){ return m_strErrorMessage; }
    /*************************************************************************/
    /* constructors */
    public LoginControl(){}

    /*************************************************************************/
    /* client methods */
    public Customer validateLogin(String email, String password){

        /* clear error message */
        m_iError = 0;
        m_strErrorMessage = "";

        /* check for nulls */
        if(email.length()==0){
                m_iError = NO_EMAIL;
                return null;
        }
        if(password.length()==0){
                m_iError = NO_PASSWORD;
                return null;
        }
        /* begin validate against database*/
        try {
            /* init connection */
            DataAccessBase.open();

            /* retrieve customer data */
            CustomersDataAccess daCustomers = new CustomersDataAccess();
            Customer cust = daCustomers.validateLogin(email, password);
            boolean bEmailExists = daCustomers.emailExists(email);

            /* close after done */
            DataAccessBase.close();

            if(cust==null){
                if(bEmailExists)
                   m_iError = WRONG_PASSWORD;
                else if(!Validation.validateEmail(email))
                   m_iError = INVALID_EMAIL;
                else
                   m_iError = EMAIL_NOT_FOUND;
            }
            return cust;
        }
        catch (DataAccessException ex1){
            m_iError = SYSTEM_ERROR;
            m_strErrorMessage = ex1.getMessage();
            return null;
        }catch (SQLException ex2){
            m_iError = SYSTEM_ERROR;
            m_strErrorMessage = ex2.getMessage();
            return null;
        }
    }
    /*************************************************************************/
    /* test */
    public static void main(String[] args){

        LoginControl lgctrl = new LoginControl();
        Customer cust = lgctrl.validateLogin("chseng@homecodeworks.com", "125678");

        if(cust!=null)
            System.out.println(cust);
        else    
            System.out.println(lgctrl.getError());
    }
}
